package cn.iocoder.yudao.module.design.controller.admin.userflowrel;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.userflowrel.vo.*;
import cn.iocoder.yudao.module.design.convert.userflowrel.UserFlowRelConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.userflowrel.UserFlowRelDO;
import cn.iocoder.yudao.module.design.service.userflowrel.UserFlowRelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 用户和流程角色关联")
@RestController
@RequestMapping("/design/user-flow-rel")
@Validated
public class UserFlowRelController {

    @Resource
    private UserFlowRelService userFlowRelService;

    @PostMapping("/create")
    @Operation(summary ="创建用户和流程角色关联")
    @PreAuthorize("@ss.hasPermission('design:user-flow-rel:create')")
    public CommonResult<Long> createUserFlowRel(@Valid @RequestBody UserFlowRelCreateReqVO createReqVO) {
        return success(userFlowRelService.createUserFlowRel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新用户和流程角色关联")
    @PreAuthorize("@ss.hasPermission('design:user-flow-rel:update')")
    public CommonResult<Boolean> updateUserFlowRel(@Valid @RequestBody UserFlowRelUpdateReqVO updateReqVO) {
        userFlowRelService.updateUserFlowRel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary ="删除用户和流程角色关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('design:user-flow-rel:delete')")
    public CommonResult<Boolean> deleteUserFlowRel(@RequestParam("id") Long id) {
        userFlowRelService.deleteUserFlowRel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得用户和流程角色关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:user-flow-rel:query')")
    public CommonResult<UserFlowRelRespVO> getUserFlowRel(@RequestParam("id") Long id) {
        UserFlowRelDO userFlowRel = userFlowRelService.getUserFlowRel(id);
        return success(UserFlowRelConvert.INSTANCE.convert(userFlowRel));
    }

    @GetMapping("/list")
    @Operation(summary ="获得用户和流程角色关联列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('design:user-flow-rel:query')")
    public CommonResult<List<UserFlowRelRespVO>> getUserFlowRelList(@RequestParam("ids") Collection<Long> ids) {
        List<UserFlowRelDO> list = userFlowRelService.getUserFlowRelList(ids);
        return success(UserFlowRelConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary ="获得用户和流程角色关联分页")
    @PreAuthorize("@ss.hasPermission('design:user-flow-rel:query')")
    public CommonResult<PageResult<UserFlowRelRespVO>> getUserFlowRelPage(@Valid UserFlowRelPageReqVO pageVO) {
        PageResult<UserFlowRelDO> pageResult = userFlowRelService.getUserFlowRelPage(pageVO);
        return success(UserFlowRelConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出用户和流程角色关联 Excel")
    @PreAuthorize("@ss.hasPermission('design:user-flow-rel:export')")
   
    public void exportUserFlowRelExcel(@Valid UserFlowRelExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UserFlowRelDO> list = userFlowRelService.getUserFlowRelList(exportReqVO);
        // 导出 Excel
        List<UserFlowRelExcelVO> datas = UserFlowRelConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户和流程角色关联.xls", "数据", UserFlowRelExcelVO.class, datas);
    }

}
